/*
* Copyright 2016 YONYOU Corporation Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project         marketing

* @Package         com.yonyou.marketing.message.push.provider

* @Author          LuZhen

* @FileName        PushThreadCallback

* @Date            2016/11/15

----------------------------------------------------------------------------------
*     Date          Who             Version         Comments
* 1. 2016/11/15        LuZhen          1.0             Create Source
*
----------------------------------------------------------------------------------
*/
package com.yonyou.gmmc.service.wechatpush.provider;


import com.google.common.util.concurrent.FutureCallback;
import com.yonyou.gmmc.service.wechatpush.mq.entrance.MessageSender;

import org.marketing.common.entity.wechatpushmessage.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component()
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PushThreadCallback implements FutureCallback<MessagePushResult>{

    private WeChatMessageProtocol protocol;

    @Autowired
    private MessageSender sender;

    public PushThreadCallback(WeChatMessageProtocol protocol) {
        this.protocol = protocol;
    }

    @Override
    public void onSuccess(MessagePushResult result) {
        if(!result.getSuccess()){
            protocol.getHandleInfo().addHandleSimpleHistory(result.getMessage());
            sender.send(protocol);
        }
    }

    @Override
    public void onFailure(Throwable t) {
        protocol.getHandleInfo().addHandleSimpleHistory(t.getMessage());
        sender.send(protocol);
    }
}
